package com.zlf.mvc.config;

import com.google.gson.Gson;
import com.zlf.constant.CrowdConstant;
import com.zlf.exception.*;
import com.zlf.util.CrowdUtil;
import com.zlf.util.ResultEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author ：张利锋
 * @date ：Created in 2020/11/12 15:32
 * @description： TOOD
 * @version: 1.0
 */
//表示这是基于注解的异常处理器
@ControllerAdvice
public class CrowdExceptionResolver {


    /**
     * create by: 张利锋
     * description: 新增role时重复报此异常
     * create time: 2020/11/15 9:58
     * @params :[request, response, exception]
     * @return ModelAndView
     */
    @ExceptionHandler(value = RoleSaveException.class)
    public ModelAndView resolveRoleSaveException(HttpServletRequest request,
                                                     HttpServletResponse response,
                                                     RoleSaveException exception) throws IOException {
        String viewName="role-page";
        return commonResolve(request, response, exception, viewName);
    }
    /**
     * create by: 张利锋
     * description: 注解方法的权限异常跳转
     * create time: 2020/11/15 9:58
     * @params :[request, response, exception]
     * @return ModelAndView
     */
    @ExceptionHandler(value = Exception.class)
    public ModelAndView resolveNoAuthException(HttpServletRequest request,
                                                     HttpServletResponse response,
                                                     Exception exception) throws IOException {
        String viewName="role-page";
        return commonResolve(request, response, exception, viewName);
    }

    /**
     * create by: 张利锋
     * description: 修改用户时如果用户名重复则抛出并返回edit页面
     * create time: 2020/11/15 9:58
     * @params :[request, response, exception]
     * @return ModelAndView
     */
    @ExceptionHandler(value = UpdateFailedException.class)
    public ModelAndView resolveUpdateFailedException(HttpServletRequest request,
                                                              HttpServletResponse response,
                                                              UpdateFailedException exception) throws IOException {
        String viewName="system-error";
        return commonResolve(request, response, exception, viewName);
    }

    /**
     * create by: 张利锋
     * description: 新增用户时如果用户名重复则抛出并返回add页面
     * create time: 2020/11/15 9:58
     * @params :[request, response, exception]
     * @return ModelAndView
     */
    @ExceptionHandler(value = LoginAcctAlreadyInUseException.class)
    public ModelAndView resolveLoginAcctAlreadyInUseException(HttpServletRequest request,
                                                              HttpServletResponse response,
                                                              LoginAcctAlreadyInUseException exception) throws IOException {
        String viewName="admin-add";
        return commonResolve(request, response, exception, viewName);
    }

//    /**
//     * create by: 张利锋
//     * description: 登录状态检测，如过没登录则抛出异常返回登录页面
//     * create time: 2020/11/13 18:53
//     *
//     * @return ModelAndView
//     * @params :[request, response, exception]
//     */
//    @ExceptionHandler(value = AccessForbidenException.class)
//    public ModelAndView resolveAccessForbidenException(HttpServletRequest request, HttpServletResponse response, LoginFailedException exception) throws IOException {
//        String viewName = "admin-login";
//        return commonResolve(request, response, exception, viewName);
//    }

    /**
     * create by: 张利锋
     * description: 登录异常处理
     * create time: 2020/11/12 20:42
     *
     * @return ModelAndView
     * @params :[request, response, exception]
     */
    @ExceptionHandler(value = LoginFailedException.class)
    public ModelAndView resolveLoginFailedException(HttpServletRequest request, HttpServletResponse response, LoginFailedException exception) throws IOException {
        String viewName = "admin-login";
        return commonResolve(request, response, exception, viewName);
    }

    private ModelAndView commonResolve(HttpServletRequest request, HttpServletResponse response, Exception e, String viewName) throws IOException {
        if (CrowdUtil.judgeRequestType(request)) {
            //ajax请求
            response.getWriter().write(new Gson().toJson(ResultEntity.failed(e.getMessage())));
            return null;
        }
        //普通请求
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject(CrowdConstant.ATTR_NAME_EXCEPTION, e);
        modelAndView.setViewName(viewName);
        return modelAndView;
    }

}
